﻿using UnityEngine;
using System.Collections;

public class Oc_FormationItem : ObjectBase {

    public int index;
    public MemberModel m_Member;

    public GameObject m_Empty;
    public GameObject m_On;
    public GameObject m_Off;

    public UILabel m_Skill;
    public UILabel m_Skill_On;

    public UISprite m_Char;
    public UISprite m_Char_On;

    public UISprite m_Bg;
    public UISprite m_Line;


    enum ItemMode
    {
        empty,
        on,
        off
    }

    ItemMode m_Mode = ItemMode.empty;

    //member null 이면 엠티
    public void SetMember(MemberModel member, FormationSortKind sortKind)
    {
        m_Member = member;       

        if (m_Member == null)
        {
            m_Mode = ItemMode.empty;
            m_Empty.SetActive(true);
            m_On.SetActive(false);
            m_Off.SetActive(false);
        }
        else
        {
            m_Empty.SetActive(false);
            m_On.SetActive(false);
            m_Off.SetActive(true);  

            PortraitControl.SetPortrait(m_Char_On, m_Member, true);
            PortraitControl.SetPortrait(m_Char, m_Member, false);

            SetSubjectAndValue(sortKind);
        }

        SetOnOff(false);
    }

    public void SetSubjectAndValue(FormationSortKind sortKind)
    {
        if (m_Member != null)
        {
            DBaseCharacter_New stats = m_Member.m_Stat.data;
         
            switch (sortKind)
            {
                case FormationSortKind.Level:
                    m_Skill.text = string.Format("{0:,0}", m_Member.level);
                    break;
                case FormationSortKind.Inhance:
                    m_Skill.text = string.Format("{0:,0}", m_Member.strengthening);
                    break;
                case FormationSortKind.HP:
                    m_Skill.text = string.Format("{0:,0}", stats.baseHP);
                    break;
                case FormationSortKind.Near_Att:
                    if (stats.atkType == 1)
                        m_Skill.text = string.Format("{0:,0}", stats.baseAtk);
                    else
                        m_Skill.text = "0";
                    break;
                case FormationSortKind.Far_Att:
                    if (stats.atkType == 2)
                        m_Skill.text = string.Format("{0:,0}", stats.baseAtk);
                    else
                        m_Skill.text = "0";
                    break;
                case FormationSortKind.Magic_Att:
                    if (stats.atkType == 3)
                        m_Skill.text = string.Format("{0:,0}", stats.baseAtk);
                    else
                        m_Skill.text = "0";
                    break;
                case FormationSortKind.Wave_Att:
                    if (stats.atkType == 4)
                        m_Skill.text = string.Format("{0:,0}", stats.baseAtk);
                    else
                        m_Skill.text = "0";
                    break;
                case FormationSortKind.Special_Att:
                    if (stats.atkType == 5)
                        m_Skill.text = string.Format("{0:,0}", stats.baseAtk);
                    else
                        m_Skill.text = "0";
                    break;
                case FormationSortKind.Near_Def:
                    m_Skill.text = string.Format("{0:,0}", stats.melee);
                    break;
                case FormationSortKind.Far_Def:
                    m_Skill.text = string.Format("{0:,0}", stats.range);
                    break;
                case FormationSortKind.Magic_Def:
                    m_Skill.text = string.Format("{0:,0}", stats.magic);
                    break;
                case FormationSortKind.Wave_Def:
                    m_Skill.text = string.Format("{0:,0}", stats.pulse);
                    break;
                case FormationSortKind.Special_Def:
                    m_Skill.text = string.Format("{0:,0}", stats.special);
                    break;
            }

            m_Skill_On.text = m_Skill.text;
        }
    }

    public void SetOnOff(bool OnOff)
    {
        if (m_Member != null)
        {
            if(OnOff)
            {
                m_Mode = ItemMode.on;

                m_Empty.SetActive(false);
                m_On.SetActive(true);
                m_Off.SetActive(false);

                UISprite bg = m_On.transform.FindChild("figure_bg").GetComponent("UISprite") as UISprite;

                Color color = Constants.CharInfoRareColorBG[m_Member.rarity];
                color.a = 150 / 255f;
                bg.color = color;
                
            }
            else
            {
                m_Mode = ItemMode.off;

                m_Empty.SetActive(false);
                m_On.SetActive(false);
                m_Off.SetActive(true);

                Color color = Constants.CharInfoRareColorBG[m_Member.rarity];
                color.a = 150 / 255f;
                m_Line.color = color;
                ////m_Bg.color = Constants.CharInfoRareColor[m_Member.rarity];
                //m_Line.color = Constants.CharInfoRareColor[m_Member.rarity];
            }
        }
    }
}
